﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ninject;

namespace RobotBattle2.Windows {
	public partial class MainForm : Form {
		Game currentGame;

		public MainForm() {
			InitializeComponent();
		}

		[Inject]
		public ScriptParserBase ScriptParser { get; set; }

		private void renderPanel_Paint(object sender, PaintEventArgs e) {
			if (currentGame == null)
				return;
			var g = e.Graphics;
			foreach (var robot in currentGame.Robots) {
				g.DrawRectangle(
					Pens.Red,
					(float)robot.X,
					(float)robot.Y,
					32,
					32
				);
			}
		}

		private void newGameButton_Click(object sender, EventArgs e) {
			if (openFileDialog1.ShowDialog() != DialogResult.OK)
				return;
			if (currentGame != null)
				stopButton_Click(null, EventArgs.Empty);
			currentGame = new Game();
			foreach (var item in openFileDialog1.FileNames) {
				var script = ScriptParser.LoadScript(item);
				currentGame.AddRobot(script);
			}
			renderPanel.Invalidate();
		}

		private void runButton_Click(object sender, EventArgs e) {
			turnTimer.Enabled =
				pauseButton.Enabled =
				stopButton.Enabled = true;
			runButton.Enabled = false;
		}

		private void pauseButton_Click(object sender, EventArgs e) {
			turnTimer.Enabled =
				pauseButton.Enabled = false;
			runButton.Enabled =
				stopButton.Enabled = true;
		}

		private void stopButton_Click(object sender, EventArgs e) {
			turnTimer.Enabled =
				runButton.Enabled =
				pauseButton.Enabled =
				stopButton.Enabled = false;
			currentGame = null;
		}

		private void turnTimer_Tick(object sender, EventArgs e) {
			currentGame.RunTurn();
			renderPanel.Invalidate();
		}
	}
}
